using System.Threading.Tasks;
using Agriculture.Api.Read.Application.Command;
using Agriculture.ErrorCode;
using MediatR;
using Microsoft.AspNetCore.Mvc;

namespace Agriculture.Api.Read.Controllers
{
    /// <summary>
    /// 用户管理
    /// </summary>
    [ApiController]
    [Route("api/[controller]")]
    public class UserController : ControllerBase
    {
        private readonly IMediator _mediator;

        public UserController(IMediator mediator)
        {
            _mediator = mediator;
        }

        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="command">登录信息</param>
        /// <returns>登录结果</returns>
        [HttpPost("login")]
        public async Task<ApiResult<UserLoginResult>> Login([FromBody] UserLoginCommand command)
        {
            if (string.IsNullOrEmpty(command.Account) || string.IsNullOrEmpty(command.Password))
            {
                return new ApiResult<UserLoginResult>
                {
                    Code = ApiEnum.Fail,
                    Msg = "用户名和密码不能为空",
                    Data = null
                };
            }

            var result = await _mediator.Send(command);

            if (result == null)
            {
                return new ApiResult<UserLoginResult>
                {
                    Code = ApiEnum.Fail,
                    Msg = "用户名或密码错误",
                    Data = null
                };
            }

            return new ApiResult<UserLoginResult>
            {
                Code = ApiEnum.Success,
                Msg = "登录成功",
                Data = result
            };
        }

        /// <summary>
        /// 获取当前用户信息
        /// </summary>
        /// <returns>当前用户信息</returns>
        [HttpGet("current")]
        public async Task<ApiResult<UserLoginResult>> GetCurrentUser()
        {
            // 此方法需要通过Token获取当前登录用户信息
            // 为了简化示例，这里只做了一个样例方法，实际实现时应该从Token中获取用户ID，然后查询数据库
            return new ApiResult<UserLoginResult>
            {
                Code = ApiEnum.Success,
                Msg = "获取成功",
                Data = null
            };
        }
    }
} 